package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.dao.FlavourSpeedDao;
import com.dlc.felear.xiaoensale.deviceControlSf.OperatorAutoHandle;
import com.dlc.felear.xiaoensale.entity.FlavourSpeed;
import com.dlc.felear.xiaoensale.entity.Operator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class HistoryOperatorShowAdapter extends BaseAdapter implements View.OnClickListener,SingleInputDialog.SingleDialogCallback {

    private static final String TAG = "OperatorItemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<Operator> items;
    private OperatorAutoHandle operatorAutoHandle;
    private FlavourSpeedDao flavourSpeedDao;
    private SingleInputDialog singleInputDialog;

    public HistoryOperatorShowAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
        flavourSpeedDao=new FlavourSpeedDao(context);
        operatorAutoHandle = OperatorAutoHandle.getInstance(null, null,TAG);
        singleInputDialog=new SingleInputDialog(context,this);
    }

    public List<Operator> getData() {
        return items;
    }


    public void setItems(List<Operator> data) {
        try {
            items.clear();
            Collections.sort(data);
            items.addAll(data);
            notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    private int getdelayTlTime(Operator operator, int value) {
        return operatorAutoHandle.mlVavluechangeMsec(operator.getName(), value) / 1000;
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Operator getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(context, R.layout.adapter_history_operator_item, null);
                holder.tv_num = convertView.findViewById(R.id.o_tv_num);
                holder.tv_name = convertView.findViewById(R.id.o_tv_operator);
                holder.tv_value = convertView.findViewById(R.id.o_tv_value);
                holder.tv_value1 = convertView.findViewById(R.id.o_tv_value1);
                holder.tv_delay = convertView.findViewById(R.id.o_tv_delay);
                holder.iv_delete = convertView.findViewById(R.id.o_iv_delete);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Operator operator = items.get(position);
            holder.tv_name.setText(operator.getName());
            holder.tv_num.setText(Integer.toString(operator.getNum()));
            holder.tv_value.setText(Integer.toString(operator.getValue()));
            holder.tv_value1.setText(Integer.toString(operator.getValue1()));
            holder.iv_delete.setOnClickListener(this);
            holder.iv_delete.setTag(position);
            holder.tv_value1.setOnClickListener(this);
            holder.tv_value1.setTag(position);
            setDelay(holder,operator);
        }catch (Exception e){
            Log.e(TAG,e.getMessage());
        }
        return convertView;
    }

    private void setDelay(ViewHolder holder,Operator operator){
        holder.tv_delay.setText(Integer.toString(operator.getDelayTime()));
        FlavourSpeed flavourSpeed=flavourSpeedDao.queryForName(operator.getName().trim());
        if(operator.getType()==Constant.ACTION_STEP_JYT&&flavourSpeed!=null){
            double delay=operatorAutoHandle.mlVavluechangeMsec(flavourSpeed.getName(),
                    operator.getValue1());
            Log.e(TAG,"delay="+delay);
            holder.tv_value.setText("---");
            holder.tv_delay.setText(Double.toString(Math.ceil(delay/1000)));
            holder.tv_value1.setText(operator.getValue1()+"ml");
        }else  if(operator.getType()==Constant.ACTION_STEP_JGT&&flavourSpeed!=null){
            double delay=operatorAutoHandle.mlVavluechangeMsec(flavourSpeed.getName(),
                    operator.getValue1());
            Log.e(TAG,"delay="+delay);
            holder.tv_value.setText("---");
            holder.tv_delay.setText(Double.toString(Math.ceil(delay/1000)));
            holder.tv_value1.setText(operator.getValue1()+"克");
        }else if(TextUtils.equals(operator.getName(), Constant.ACTION_NAME_KQJR)
                ||TextUtils.equals(operator.getName(), Constant.ACTION_NAME_TZJR)){
            holder.tv_value.setText(operator.getValue()+"档");
            holder.tv_value1.setText(operator.getValue1()+"℃");
        }else if(TextUtils.equals(operator.getName(), Constant.ACTION_NAME_GSGZ)
                ||TextUtils.equals(operator.getName(), Constant.ACTION_NAME_DKGZ)){
            holder.tv_value.setText("--");
            holder.tv_value1.setText("--");
        }else if(TextUtils.equals(operator.getName(), Constant.ACTION_NAME_KSJB)
                ||TextUtils.equals(operator.getName(), Constant.ACTION_NAME_TZJB)){
            holder.tv_value.setText("--");
            holder.tv_value1.setText(operator.getValue1()+"档");
        }else if(TextUtils.equals(operator.getName(), Constant.ACTION_NAME_JSC)){
            holder.tv_value.setText("格子号:"+operator.getValue());
            holder.tv_value1.setText("---");
        }else if(TextUtils.equals(operator.getName(), Constant.ACTION_NAME_CCJD)){
            holder.tv_value.setText("炒菜位置:"+operator.getValue()+"档");
        }
    }


    @Override
    public void onClick(View v) {
        try {
            if(R.id.o_iv_delete==v.getId()){
                int p=(int)v.getTag();
                items.remove(p);
                notifyDataSetChanged();
            }
            if(R.id.o_tv_value1==v.getId()){
                int p=(int)v.getTag();
                setValue1(p);
            }

        } catch (Exception e) {
            Log.e(TAG, "异常信息:" + e.getMessage());
        }
    }

    private void setValue1(int p){
        Operator operator=items.get(p);
        if(operator.getType()==Constant.ACTION_STEP_JYT||operator.getType()==Constant.ACTION_STEP_JGT){
            singleInputDialog.showDialog(p);
            singleInputDialog.setTips("值");
            singleInputDialog.setHintMsg("输入调料的重量");
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        Collections.sort(items);
    }

    @Override
    public void SingleDialogInputResult(String value, int id) {
        Operator operator=items.get(id);
        operator.setValue1(Integer.parseInt(value));
        items.remove(id);
        items.add(id,operator);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tv_num, tv_name, tv_value, tv_value1, tv_delay;
        ImageView iv_delete;
    }

    private void showMsg(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}